/*
 * To change this template, choose Tools | Templates
 * AND open the template in the editor.
 */

package modelos;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author usuario
 */
public class ModeloIngrediente extends ModeloDB{

    private int id;
    private String descripcion,status;
    private float precio, stock;

    public ModeloIngrediente(int id, String descripcion, String status, float precio, float stock) {
        super();
        this.id = id;
        this.descripcion = descripcion;
        this.status = status;
        this.precio = precio;
        this.stock = stock;
    }

    public ModeloIngrediente() {
        super();
    }

    public boolean registar(){
        conectar();
        String sql="INSERT INTO Ingrediente (id, descripcion, precio, stock, status) VALUES(?,?,?,?,?)";
        PreparedStatement stam = crearPreparedStatement(sql);
        try {
            stam.setInt(1, getId());
            stam.setString(2, getDescripcion().toUpperCase());
            stam.setFloat(3, getPrecio());
            stam.setFloat(4, getStock());
            stam.setString(5, getStatus());
        } catch (SQLException ex) {
            Logger.getLogger(ModeloIngrediente.class.getName()).log(Level.SEVERE, null, ex);
        }
        boolean sw = ejecutarQueryPrepare(stam);
        desconectar();
        return sw;
    }

 public boolean eliminar(){
     conectar();
     String sql="UPDATE Ingrediente SET status = 'E' WHERE id=?  AND status = 'A'";
     PreparedStatement stam = crearPreparedStatement(sql);
     try {
         stam.setInt(1, getId());
     } catch (SQLException ex) {
            Logger.getLogger(ModeloIngrediente.class.getName()).log(Level.SEVERE, null, ex);
     }
     boolean sw = ejecutarQueryPrepare(stam);
     desconectar();
     return sw;
 }

  public boolean actualizar(){
     conectar();
     String sql="UPDATE Ingrediente SET descripcion =?, precio=?, stock=? WHERE id=?  AND status = 'A'";
     PreparedStatement stam = crearPreparedStatement(sql);
     try {
         stam.setString(1,getDescripcion());
         stam.setFloat(2, getPrecio());
         stam.setFloat(3, getStock());
         stam.setInt(4, getId());
     } catch (SQLException ex) {
            Logger.getLogger(ModeloIngrediente.class.getName()).log(Level.SEVERE, null, ex);
     }
     boolean sw = ejecutarQueryPrepare(stam);
     desconectar();
     return sw;
 }

  public boolean  consultar(){
    conectar();
    boolean sw = false;
    String sql="SELECT * FROM Ingrediente WHERE id=? AND status='A'";
    PreparedStatement stam = crearPreparedStatement(sql);
     try {stam.setInt(1, getId());
     } catch (SQLException ex) {
            Logger.getLogger(ModeloIngrediente.class.getName()).log(Level.SEVERE, null, ex);
     }
    ResultSet rs= ejecutarQuery(stam);
    try{
        if (rs.next()){
            sw = true;
            setDescripcion(rs.getString("descripcion"));
            setId(rs.getInt("id"));
            setPrecio(rs.getFloat("precio"));
            setStatus(rs.getString("status"));
            setStock(rs.getFloat("stock"));
         }
    } catch (SQLException ex) {
        Logger.getLogger(ModeloIngrediente.class.getName()).log(Level.SEVERE, null, ex);
    }
    return sw;
  }

  public Vector<ModeloDB> listar(){
    Vector<ModeloDB> v = new Vector<ModeloDB>();
    String sql="SELECT * FROM Ingrediente WHERE status='A'";
    conectar();
    ResultSet rs= crearQuery(sql);
    try{
        while (rs.next()){
            ModeloIngrediente  i = new ModeloIngrediente();
            i.setDescripcion(rs.getString("descripcion"));
            i.setId(rs.getInt("id"));
            i.setPrecio(rs.getFloat("precio"));
            i.setStatus(rs.getString("status"));
            i.setStock(rs.getFloat("stock"));
            v.add(i);
         }
    } catch (SQLException ex) {
        Logger.getLogger(ModeloIngrediente.class.getName()).log(Level.SEVERE, null, ex);
    }
    return v;
  }

  public Vector<String> listarProdcutos(){
      System.out.println(id);
    Vector<String> v = new Vector<String>();
    String sql="select Producto.descripcion from  Producto,IngredientexProducto,Ingrediente where Ingrediente.status='A' and IngredientexProducto.status='A' and Producto.status='A' and Ingrediente.id=IngredientexProducto.id_ingrediente and Ingrediente.id=?";
    conectar();
    PreparedStatement stmn = crearPreparedStatement(sql);
    try {
        stmn.setInt(1, getId());
    } catch (SQLException ex) {
            Logger.getLogger(ModeloIngrediente.class.getName()).log(Level.SEVERE, null, ex);
    }
    ResultSet rs= ejecutarQuery(stmn);
    try{
        while (rs.next())
            v.add(rs.getString(1));
    } catch (SQLException ex) {
        Logger.getLogger(ModeloIngrediente.class.getName()).log(Level.SEVERE, null, ex);
    }
    desconectar();
      System.out.println(v.size());
    return v;
  }

      public boolean verificarStock(float porcion){
          consultar();
          if((getStock()-porcion)>0)
              return true;
          else
              return false;
      }

     public void actualizarStock(float porcion){
       conectar();
        setStock(getStock()-porcion);
        String sql="UPDATE Ingrediente SET stock=? WHERE id=?  AND status = 'A'";
        PreparedStatement stam = crearPreparedStatement(sql);
        try {
            stam.setFloat(1, getStock());
            stam.setInt(2, getId());
            ejecutarQueryPrepare(stam);
        } catch (SQLException ex) {
            Logger.getLogger(ModeloIngrediente.class.getName()).log(Level.SEVERE, null, ex);
        }
        desconectar();
    }
  public String getDescripcion() {
        return descripcion;
    }

    public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
    }

    public float getPrecio() {
        return precio;
    }

    public void setPrecio(float precio) {
        this.precio = precio;
    }

    public float getStock() {
        return stock;
    }

    public void setStock(float stock) {
        this.stock = stock;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }


}
